﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace ASBCC.MailService
{
    public static class Config
    {
        private static AppSettingsReader appSettingsReader = new AppSettingsReader();

        /// <summary>
        /// 数据库连接字符串
        /// </summary>
        public static string ConnectionString
        {
            get
            {
                if (appSettingsReader.GetValue("ConnectionString", typeof(string)) != null
                    && appSettingsReader.GetValue("ConnectionString", typeof(string)).ToString() != String.Empty)
                {
                    return appSettingsReader.GetValue("ConnectionString", typeof(string)).ToString();
                }
                else
                {
                    throw new Exception("从配置文件App.Config读取ConnectionString出错！");
                }
            }
        }
        /// <summary>
        /// 是否使用LDAP
        /// </summary>
        public static bool EncryptConnectionString
        {
            get
            {
                if (appSettingsReader.GetValue("EncryptConnectionString", typeof(bool)) != null
                    && appSettingsReader.GetValue("EncryptConnectionString", typeof(bool)).ToString() != String.Empty)
                {
                    return Convert.ToBoolean(appSettingsReader.GetValue("EncryptConnectionString", typeof(bool)));
                }
                else
                {
                    throw new Exception("从配置文件Web.Config读取EncryptConnectionString出错！");
                }
            }
        }
        /// <summary>
        /// 间隔分钟数
        /// </summary>
        public static int IntervalMinutes
        {
            get
            {
                if (appSettingsReader.GetValue("IntervalMinutes", typeof(int)) != null
                    && appSettingsReader.GetValue("IntervalMinutes", typeof(int)).ToString() != String.Empty)
                {
                    return Convert.ToInt32(appSettingsReader.GetValue("IntervalMinutes", typeof(int)));
                }
                else
                {
                    throw new Exception("从配置文件App.Config读取IntervalMinutes出错！");
                }
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public static int MailAmount
        {
            get
            {
                if (appSettingsReader.GetValue("MailAmount", typeof(int)) != null
                    && appSettingsReader.GetValue("MailAmount", typeof(int)).ToString() != String.Empty)
                {
                    return Convert.ToInt32(appSettingsReader.GetValue("MailAmount", typeof(int)));
                }
                else
                {
                    throw new Exception("从配置文件App.Config读取MailAmount出错！");
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public static bool LiveMode
        {
            get
            {
                if (appSettingsReader.GetValue("LiveMode", typeof(bool)) != null
                    && appSettingsReader.GetValue("LiveMode", typeof(bool)).ToString() != String.Empty)
                {
                    return Convert.ToBoolean(appSettingsReader.GetValue("LiveMode", typeof(bool)));
                }
                else
                {
                    throw new Exception("从配置文件Web.Config读取LiveMode出错！");
                }
            }
        }
        /// <summary>
        /// 测试模式下接收电子邮件地址
        /// </summary>
        public static string TestModeRecipients
        {
            get
            {
                if (appSettingsReader.GetValue("TestModeRecipients", typeof(string)) != null
                    && appSettingsReader.GetValue("TestModeRecipients", typeof(string)).ToString() != String.Empty)
                {
                    return appSettingsReader.GetValue("TestModeRecipients", typeof(string)).ToString();
                }
                else
                {
                    throw new Exception("从配置文件App.Config读取TestModeRecipients出错！");
                }
            }
        }

        /// <summary>
        /// 发送者电子邮件地址
        /// </summary>
        public static string Sender
        {
            get
            {
                if (appSettingsReader.GetValue("Sender", typeof(string)) != null
                    && appSettingsReader.GetValue("Sender", typeof(string)).ToString() != String.Empty)
                {
                    return appSettingsReader.GetValue("Sender", typeof(string)).ToString();
                }
                else
                {
                    throw new Exception("从配置文件App.Config读取Sender出错！");
                }
            }
        }
        /// <summary>
        /// 显示名称
        /// </summary>
        public static string SenderDisplayName
        {
            get
            {
                if (appSettingsReader.GetValue("SenderDisplayName", typeof(string)) != null
                    && appSettingsReader.GetValue("SenderDisplayName", typeof(string)).ToString() != String.Empty)
                {
                    return appSettingsReader.GetValue("SenderDisplayName", typeof(string)).ToString();
                }
                else
                {
                    throw new Exception("从配置文件App.Config读取SenderDisplayName出错！");
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public static string Host
        {
            get
            {
                if (appSettingsReader.GetValue("Host", typeof(string)) != null
                    && appSettingsReader.GetValue("Host", typeof(string)).ToString() != String.Empty)
                {
                    return appSettingsReader.GetValue("Host", typeof(string)).ToString();
                }
                else
                {
                    throw new Exception("从配置文件App.Config读取TestModeRecipients出错！");
                }
            }
        }

        /// <summary>
        /// SMTP用户名
        /// </summary>
        public static string UserName
        {
            get
            {
                if (appSettingsReader.GetValue("UserName", typeof(string)) != null
                    && appSettingsReader.GetValue("UserName", typeof(string)).ToString() != String.Empty)
                {
                    return appSettingsReader.GetValue("UserName", typeof(string)).ToString();
                }
                else
                {
                    throw new Exception("从配置文件App.Config读取TestModeRecipients出错！");
                }
            }
        }

        /// <summary>
        /// SMTP密码
        /// </summary>
        public static string Password
        {
            get
            {
                if (appSettingsReader.GetValue("Password", typeof(string)) != null
                    && appSettingsReader.GetValue("Password", typeof(string)).ToString() != String.Empty)
                {
                    return appSettingsReader.GetValue("Password", typeof(string)).ToString();
                }
                else
                {
                    throw new Exception("从配置文件App.Config读取Password出错！");
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public static string LogFile
        {
            get
            {
                if (appSettingsReader.GetValue("LogFile", typeof(string)) != null
                    && appSettingsReader.GetValue("LogFile", typeof(string)).ToString() != String.Empty)
                {
                    return appSettingsReader.GetValue("LogFile", typeof(string)).ToString();
                }
                else
                {
                    throw new Exception("从配置文件App.Config读取LogFile出错！");
                }
            }
        }

        /// <summary>
        /// 是否记录发送成功日志
        /// </summary>
        public static bool SaveSucceedLog
        {
            get
            {
                if (appSettingsReader.GetValue("SaveSucceedLog", typeof(bool)) != null
                    && appSettingsReader.GetValue("SaveSucceedLog", typeof(bool)).ToString() != String.Empty)
                {
                    return Convert.ToBoolean(appSettingsReader.GetValue("SaveSucceedLog", typeof(bool)));
                }
                else
                {
                    throw new Exception("从配置文件Web.Config读取SaveSucceedLog出错！");
                }
            }
        }

        /// <summary>
        /// 是否记录发送失败日志
        /// </summary>
        public static bool SaveFailedLog
        {
            get
            {
                if (appSettingsReader.GetValue("SaveFailedLog", typeof(bool)) != null
                    && appSettingsReader.GetValue("SaveFailedLog", typeof(bool)).ToString() != String.Empty)
                {
                    return Convert.ToBoolean(appSettingsReader.GetValue("SaveFailedLog", typeof(bool)));
                }
                else
                {
                    throw new Exception("从配置文件Web.Config读取SaveFailedLog出错！");
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public static bool EnableSsl
        {
            get
            {
                if (appSettingsReader.GetValue("EnableSsl", typeof(bool)) != null
                    && appSettingsReader.GetValue("EnableSsl", typeof(bool)).ToString() != String.Empty)
                {
                    return Convert.ToBoolean(appSettingsReader.GetValue("EnableSsl", typeof(bool)));
                }
                else
                {
                    throw new Exception("从配置文件Web.Config读取EnableSsl出错！");
                }
            }
        }
    }
}
